package com.kuang.web.admin;

import com.kuang.pojo.Blog;
import com.kuang.pojo.Comment;
import com.kuang.pojo.VoBlogTitle;
import com.kuang.service.BlogService;
import com.kuang.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/admin")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private BlogService blogService;

    @RequestMapping("/tocomment")
    public String tocomment(Model model, @RequestParam(defaultValue = "1") Integer pageindex) {
//         pageindex = 1;  当前页数
        Integer pagesize = 10; // 每页显示多少个！
        Integer page = 0; // 共有多少页
        int countComment = commentService.countComment();
        page = countComment % pagesize == 0 ? countComment / pagesize : (countComment / pagesize) + 1;
        List<Blog> bloglist = new ArrayList<>();
        List<VoBlogTitle> voBlogTitleList = new ArrayList<>();
        // 后台展示
        List<Comment> commentList = commentService.selectCommentPage((pageindex - 1) * pagesize, pagesize);
//        blogService.selectBlogById()

        for (int i = 0; i < commentList.size(); i++) { // 查出的博客id来查找博客对象
            Blog blog = blogService.selectBlogById(commentList.get(i).getBlogId());
            if (blog != null) {
                VoBlogTitle voBlogTitle = new VoBlogTitle();
                voBlogTitle.setCommentId(commentList.get(i).getId());  // 获取评论的id 因为后台页面展示需要通过评论的唯一id来战士
                voBlogTitle.setTitle(blog.getTitle());  // 获取博客的标题
                voBlogTitleList.add(voBlogTitle);
            }
        }
        model.addAttribute("page", page);
        model.addAttribute("pageindex", pageindex);
        model.addAttribute("countcomment", countComment);
        model.addAttribute("voblogtitlelist", voBlogTitleList);
        model.addAttribute("commentlist", commentList);
        return "admin/comment-mgr";
    }


    @RequestMapping("/deletecomment")
    @ResponseBody
    public String deletecomment(Integer commentid) {
        int i = commentService.deletecommentById(commentid);
        if (i > 0) {
            return "success";
        } else {
            return "error";
        }
        //        return "redirect:/admin/tocomment";
    }

    //    @ResponseBody
    @RequestMapping("/addcomment")
    public String addcomment(Comment comment, String isadmin) {
        comment.setCreateTime(new Date());
        File file = new File("C:\\upload02");
        String[] list = file.list();
        Random random = new Random();
        int i = random.nextInt(list.length);
        comment.setAvatar(list[i]);
        if (isadmin.equals("")) {
            comment.setAdminComment(false);  // 说明不是管理员的评论
            int num = commentService.insertComment(comment);
//            System.out.println("num = " + num);
//            System.out.println("comment = " + comment);
//            System.out.println("isadmin = " + isadmin);
        } else {
            comment.setAdminComment(true);
            int num = commentService.insertComment(comment);
//            System.out.println("num = " + num);
//            System.out.println("comment = " + comment);
//            System.out.println("isadmin = " + isadmin);
        }
        return "redirect:/toblogdetail?blogid=" + comment.getBlogId();
    }
}
